package com.oasys.controller;

import com.oasys.annotations.RequiredToken;
import com.oasys.entity.LoginUser;
import com.oasys.entity.UserLoginVo;
import com.oasys.entity.vo.UserAccountVo;
import com.oasys.enums.OaResponseCode;
import com.oasys.service.SysUserService;
import com.oasys.utils.ResponseResult;
import com.oasys.utils.VerifyCodeUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * @author mis_wu
 */
@RequestMapping("/user")
@RestController
@Api(tags = "用户信息")
@Slf4j
public class UserController {

    @Resource
    private SysUserService sysUserService;

    /**
     * login
     * @param userLoginVo user
     * @return res
     */
    @PostMapping("/userLogin")
    @ApiOperation("用户登录")
    @RequiredToken
    public ResponseResult<Map<String,Object>> userLogin(@RequestBody UserLoginVo userLoginVo,HttpSession session){
        try {
            Map<String,Object> map = new HashMap<>(2);
            String code = (String) session.getAttribute("imgCode");
            log.info("登录信息:{},code:{}",userLoginVo.toString(),code);
            if (userLoginVo.getImgCode().equals(code)){
                //map.put("msg","验证码错误!!!");
                //return new ResponseResult<>(OaResponseCode.FAIL.getCode(),OaResponseCode.FAIL.getMsg(),map);
            }
            String token = sysUserService.signIn(userLoginVo.getUserName(), userLoginVo.getPassword());
            LoginUser loginUser = sysUserService.getLoginUser();
            map.put("token",token);
            map.put("currUser",loginUser);
            return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),OaResponseCode.SUCCESS.getMsg(),map);
        }catch (Exception e){
            log.error(e.getMessage(), e);
            throw e;
        }
    }

    /**
     * 生成验证码
     * @RequiredToken 在方法上添加表示不拦截
     */
    @ApiOperation("生成验证码")
    @GetMapping("/getImageCode")
    @RequiredToken
    public void getImage(HttpSession session ,HttpServletResponse response) throws IOException {
        //生成验证码
        String securityCode = VerifyCodeUtils.getSecurityCode();
        //将验证码放入session
        String id = session.getId();
        log.info("session id:{}",id);
        session.setAttribute("imgCode",securityCode);
        //生成图片
        BufferedImage image = VerifyCodeUtils.createImage(securityCode);
        //输出图片
        ServletOutputStream outputStream = response.getOutputStream();
        //调用工具类
        ImageIO.write(image,"png",outputStream);
        log.info("获取验证码成功!!!");
    }

    @PostMapping("/userAccountAssignment")
    @ApiOperation("用户账号分配")
    public ResponseResult<Boolean> userAccountAssignment(@RequestBody UserAccountVo userAccountVo){
        Boolean result = sysUserService.userAccountAssignment(userAccountVo);
        return new ResponseResult<>(OaResponseCode.SUCCESS.getCode(),null,result);
    }

}
